﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication5
{

    public partial class Form1 : Form
    {
        System.Data.OleDb.OleDbConnection connex = new System.Data.OleDb.OleDbConnection();
        bool vacio = true;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            connex.ConnectionString = @"PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Familia\Desktop\notas1.mdb";
            connex.Open();
            OleDbCommand consulta = new OleDbCommand("select fecha from Tabla1 WHERE avisada=0",connex);
            OleDbDataReader lector = consulta.ExecuteReader();
            while (lector.Read())
            {
                string fecha = lector["fecha"] + " 00:00:00";
                DateTime fecha1 = new DateTime();
                fecha1 = Convert.ToDateTime(fecha);
                monthCalendar1.AddBoldedDate(fecha1);
                monthCalendar1.UpdateBoldedDates();
            }
                

        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            string fecha = monthCalendar1.SelectionStart.ToShortDateString();
            label2.Text = "Dia : "+fecha;
            string query = "select * FROM Tabla1 WHERE fecha='"+fecha+"';";
            OleDbCommand sql = new OleDbCommand(query, connex);
            OleDbDataReader lector = sql.ExecuteReader();
            vacio = true;
            textBox1.Text = "";
            while (lector.Read())
            {
                
                textBox1.Text = lector["contenido"].ToString();
                vacio = false;
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            string Fecha = monthCalendar1.SelectionStart.ToShortDateString();
            string contenido = textBox1.Text;
            monthCalendar1.AddBoldedDate(monthCalendar1.SelectionStart);
            monthCalendar1.UpdateBoldedDates();
            string sqlquery;
            if (vacio == true)
            {
                sqlquery = "INSERT INTO Tabla1(fecha,contenido,avisada) VALUES('" + Fecha + "','" + contenido + "',0);";
            }
            else
            {
                sqlquery = "UPDATE Tabla1 SET contenido='" + contenido + "' WHERE fecha='" + Fecha + "';";
            }
            OleDbCommand consulta = new OleDbCommand(sqlquery,connex);
            consulta.ExecuteNonQuery();
        

        }

        private void holaToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            notifyIcon1.Visible = true;
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            notifyIcon1.Visible = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            string hoy = monthCalendar1.TodayDate.ToShortDateString();
            string sqlquery = "SELECT * FROM Tabla1 WHERE avisada=0";
            OleDbCommand consulta = new OleDbCommand(sqlquery, connex);
            OleDbDataReader lector = consulta.ExecuteReader();
            while (lector.Read())
            {
                
                if (lector["fecha"].ToString() == hoy)
                {
                    MessageBox.Show(lector["Contenido"].ToString(), "Alerta");
                    DialogResult desactivaralerta = MessageBox.Show("Desactivar alerta?", "Desactivar alerta?", MessageBoxButtons.YesNo);
                    if (desactivaralerta == DialogResult.Yes)
                    {
                        sqlquery = "UPDATE Tabla1 SET avisada=1 WHERE fecha='" + hoy + "';";
                        OleDbCommand nueva = new OleDbCommand(sqlquery, connex);
                        nueva.ExecuteNonQuery();
                    }
                }
            }
            timer1.Enabled = true;        
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult resultado = MessageBox.Show("Estas seguro?", "Estas seguro?", MessageBoxButtons.YesNo);
            if (resultado == DialogResult.Yes)
            {

                string fecha = monthCalendar1.SelectionStart.ToShortDateString();
                string sqlquery = "DELETE * FROM Tabla1 WHERE fecha='" + fecha + "';";
                OleDbCommand query = new OleDbCommand(sqlquery, connex);
                query.ExecuteNonQuery();
                monthCalendar1.RemoveBoldedDate(monthCalendar1.SelectionStart);
                monthCalendar1.UpdateBoldedDates();
                textBox1.Text = "";
            }
            else
            {
                MessageBox.Show("Te salvaste en el ultimo momento");
            }
        }
    }
}
